
/**
 * Module dependencies.
 */

var express = require('express')
        , routes = require('./routes')
        , announcement = require('./routes/announcement')
        , user = require('./routes/user')
        , login = require('./routes/login')
        , quicknav = require('./routes/quicknav')
        , online = require('./routes/online')
        , comment = require('./routes/comment')
        , ws = require('./routes/workspace')
        , wf = require('./routes/workflow')
        , form = require('./routes/form')
        , tables = require('./routes/tables')
        , reports = require('./routes/reports')
        , request = require('./routes/loadrequest')
        , requestview = require('./routes/requestview')
        , http = require('http')
        , path = require('path');

var app = express();

//  authenticator
var authUser = function(req, res, next) {
    if (!req.session.user) {
        res.send('You are not authorized to access this page/resource, please login.');
    } else {
        next();
    }
};

var authUserDirect = function(req, res, next) {
    if (!req.session.user) {
        res.redirect('/login');
    } else {
        next();
    }
}; 

var authAdmin = function(req, res, next) {
    if (req.session.user) {
        if ((req.session.user.role).toLowerCase() !== 'admin') {
            res.send('Login as admin to get this resource');
        } else {
            next();
        }
    } else {
        res.send('You are not authorized to access this page/resource, please login.');
    }
};

app.configure(function() {
    app.set('port', process.env.PORT || 3000);
    app.set('views', __dirname + '/views');
    app.set('view engine', 'jade');
    app.use(express.favicon());
    app.use(express.logger('dev'));
    app.use(express.bodyParser());
    app.use(express.methodOverride());
    app.use(express.cookieParser());
    app.use(express.session({secret: 'formalistics_key'}));
    app.use(app.router);
    app.use(express.static(path.join(__dirname, 'public')));
});

app.configure('development', function() {
    app.use(express.errorHandler());
});

app.get('/', authUserDirect, routes.index);
app.get('/announcement', authUser, announcement.getAnnouncement);
app.post('/announcement/:announcement', authUser, announcement.save); // save comment to db.
app.get('/workspace',authUserDirect, ws.load);
app.post('/workspace',authUserDirect, ws.save);
app.get('/workflow', wf.load);
app.get('/form/:ID/:requestID', authUser, form.load);
app.post('/form/:ID/:requestID', authUser, form.save);
app.get('/loadrecord/:tblname/:id', authUser, request.load);  // pa check kung user lang talaga to ^^
app.get('/tables', authUser, tables.load);
app.get('/reports', authAdmin, reports.load);
app.get('/recordview/:id/:tblname/:formName', authUser, requestview.load);
app.get('/login', login.load);
app.get('/quicknav', authUser, quicknav.load);
app.get('/online', authUser, online.load);
app.get('/comment/:tableID/:requestID/:comment', authUser, comment.load);

//------------------------------------------------------------------------------
//  REST Service Routes

//      - Authenticators
app.post('/login', user.login);
app.get('/logout', function(req, res) {
    delete req.session.user;
    res.redirect('/');
});

//      - User Resource User/Admin side

app.get('/users', authUser, user.all);
app.get('/company/:companyname/availableusers', authUser, user.availableInCompany);
app.get('/company/:companyname/users', authUser, user.byCompany);

app.get('/makeavailable', authUser, user.makeAvailable);
app.get('/makeunavailable', authUser, user.makeUnavailable);

//      - User Resource Admin Side

app.post('/registeruser', authAdmin, user.register);

http.createServer(app).listen(app.get('port'), function() {
    console.log("Express server listening on port " + app.get('port'));
});
